package com.yy.cleaner.controller;
import com.yy.cleaner.common.BaseReponse;
import com.yy.cleaner.common.ErroConstant;
import com.yy.cleaner.constant.ReponseCode;
import com.yy.cleaner.model.dto.UserLoginRequest;
import com.yy.cleaner.model.pojo.LoginUser;
import com.yy.cleaner.model.pojo.User;
import com.yy.cleaner.service.UserService;
import com.yy.cleaner.utils.JwtUtil;
import com.yy.cleaner.utils.RedisCache;
import com.yy.cleaner.utils.ReponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    /*
    * 用户的登陆接口
    * */
    @PostMapping("/login")
    public BaseReponse login(@Validated @RequestBody UserLoginRequest loginRequest){
        log.info("loginRequest"+loginRequest);
        //1.验证传过来的账号密码的合法性
        if(loginRequest.getUserName().length() > 10 || loginRequest.getPassWord().length() > 10){
            //长度不合法
            return ReponseUtils.fail(ReponseCode.ERRO_SERVICE, ErroConstant.ACCOUNT_OR_PASSWORD_WRONG.getMessage());
        }
        //2.登录
        HashMap map = userService.userLogin(loginRequest);
        return ReponseUtils.success("登陆成功", map);
    }

    /**
     * 用户退出登录接口
     */
    @GetMapping("/loginout")
    public BaseReponse loginOut(){
        userService.userLoginOut();
        return ReponseUtils.success("退出登录成功",null);
    }

    /**
     * 测试接口
     */
    @GetMapping("/hello")
    public BaseReponse hello(){
        log.info("调用测试接口");
        return ReponseUtils.success("请求测试接口成功","hello");
    }

}
